38+49
.05^2
.03^2
((.53/(.05^2)) + (.6/(.03^2))) / ((1/(.05^2)) + (1/(.03^2)))
((1/(.05^2)) + (1/(.03^2)))^(-1)
sqrt((1/(.05^2)) + (1/(.03^2)))^(-1)
sqrt((1/(.05^2)) + (1/(.03^2)))^-1
sqrt(((1/(.05^2)) + (1/(.03^2)))^-1)
.025*2
((.53/(.05^2)) + (.6/(.05^2))) / ((1/(.05^2)) + (1/(.05^2)))
sqrt(((1/(.05^2)) + (1/(.05^2)))^-1)
.58 - (2*.026)
.58 - (3*.026)
.58 - (2*.035)
install.packages("flexmix")
?flexmix
library(flexmix)
?flexmix
install.packages("mclust")
library(mclust)
?mclust
??mclust
library(seatsvotes)
mvnmix
?lmer
install.packages("LMERConvenienceFunctions")
?lmer
install.packages("lmerTest")
library(lmer)
library(XML)
url<- "http://es.wikipedia.org/wiki/Anexo:Municipios_de_la_Cruzada_contra_el_Hambre"
cruzada <- readHTMLTable(url, skip.rows=1:2)
cruzada <- as.data.frame(cruzada)
cruzada <- rbind(as.matrix(cruzada[,1:3]),as.matrix(cruzada[,4:6]))
cruzada <- data.frame(ID=cruzada[,1], cruzada=rep(1, nrow(cruzada)))
View(cruzada)
# Fatalities per 100,000 Population
fatality.rate <- c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64)
names(fatality.rate) <- seq(2011:1994)
fix(fatality.rate)
seq(2011:1994)
seq(1994:2011)
?seq
2011:1994
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64)
names(fatality.rate) <- 2011:1994
fatality.rate
fatality.rate <- c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64)
names(fatality.rate) <- 2011:1994
fatality.rate
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
fatality.rate
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
lemons <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
cbind(fatality.rate, lemons)
?cbind
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
lemons <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
names(lemons) <- 1994:2001
cbind(fatality.rate[1:length(lemons)], lemons)
plot(fatality.rate, lemons)
data <- cbind(fatality.rate[1:length(lemons)], lemons)
plot(data)
data <- cbind(lemons, fatality.rate[1:length(lemons)], )
plot(data)
data <- cbind(lemons, fatality.rate[1:length(lemons)])
plot(data)
data
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
# http://www.fas.usda.gov/gats/default.aspx
# 0805302000
lemons <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7,1006.9,2746.4,12703.8,12501.6,15690.8,40402.1,17951.6,24269.5,25742.8,27210.1)
names(lemons) <- 1994:2011
data <- cbind(lemons, fatality.rate)
plot(data)
?text
data
names(data)
rownames(data)
plot(data)
text(data, labels=rownames(data))
plot(data)
text(data, labels=rownames(data), pos=4, cex=.5)
?lm
lm(fatality.rate ~ lemons, data=data)
lm(fatality.rate ~ lemons, data=as.data.frame(data))
library(arm)
display(lm(fatality.rate ~ lemons, data=as.data.frame(data)))
plot(fatality.rate[1:length(lemons.94.01)], lemons.94.01)
fatality.rate[1:length(lemons.94.01)]
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
# http://www.fas.usda.gov/gats/default.aspx
# 0805302000 LEMONS, FRESH
lemons.94.01 <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
names(lemons.94.01) <- 1994:2001
# 0805502000 LEMONS, FRESH/DR
lemons.02.11 <- c(1006.9,2746.4,12703.8,12501.6,15690.8,40402.1,17951.6,24269.5,25742.8,27210.1)
names(lemons.02.11) <- 2002:2011
plot(fatality.rate[1:length(lemons.94.01)], lemons.94.01)
fatality.rate[names(fatality.rate) %in% 1994:2001]
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
# http://www.fas.usda.gov/gats/default.aspx
# 0805302000 LEMONS, FRESH
lemons.94.01 <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
names(lemons.94.01) <- 1994:2001
# 0805502000 LEMONS, FRESH/DR
lemons.02.11 <- c(1006.9,2746.4,12703.8,12501.6,15690.8,40402.1,17951.6,24269.5,25742.8,27210.1)
names(lemons.02.11) <- 2002:2011
data1 <- cbind(fatality.rate[names(fatality.rate) %in% 1994:2001],
lemons.94.01)
data2 <- cbind(fatality.rate[names(fatality.rate) %in% 2002:2011],
lemons.02.11)
plot(data1)
# Fatalities per 100,000 Population
# http://www-fars.nhtsa.dot.gov/Main/index.aspx
fatality.rate <- rev(c(10.39,10.67,11.05,12.31,13.70,14.31,14.72,14.63,14.78,14.95,14.81,14.87,15.30,15.36,15.69,15.86,15.91,15.64))
names(fatality.rate) <- 1994:2011
# http://www.fas.usda.gov/gats/default.aspx
# 0805302000 LEMONS, FRESH
lemons.94.01 <- c(303.4,224.3,233.0,285.1,362.6,436.7,528.5,600.7)
names(lemons.94.01) <- 1994:2001
# 0805502000 LEMONS, FRESH/DR
lemons.02.11 <- c(1006.9,2746.4,12703.8,12501.6,15690.8,40402.1,17951.6,24269.5,25742.8,27210.1)
names(lemons.02.11) <- 2002:2011
data1 <- cbind(lemons.94.01,
fatality.rate[names(fatality.rate) %in% 1994:2001])
data2 <- cbind(lemons.02.11,
fatality.rate[names(fatality.rate) %in% 2002:2011])
plot(data1)
text(data1, labels=rownames(data1), pos=4, cex=.5)
plot(data2)
text(data2, labels=rownames(data2), pos=4, cex=.5)
data1
library(maps)
?smooth.maps
?smooth.map
data(state, package = "datasets")
data(votes.repub)
z = votes.repub[, "1900"]
m = map("state", fill = TRUE, plot = FALSE)
fit = smooth.map(m, z, span = 1/100, merge = TRUE, ave = TRUE)
mat = tapply(fit$z, fit[1:2], mean)
gray.colors <- function(n) gray(rev(0:(n - 1))/n)
par(bg = "blue")
filled.contour(mat, color.palette = gray.colors, nlev = 32, asp = 1)
# another way to visualize:
image(mat, col = gray.colors(100))
fit = smooth.map(m, z, span = 1/100, merge = TRUE, ave = TRUE)
mat = tapply(fit$z, fit[1:2], mean)
gray.colors <- function(n) gray(rev(0:(n - 1))/n)
par(bg = "blue")
filled.contour(mat, color.palette = gray.colors, nlev = 32, asp = 1)
require(graphics); require(grDevices)
x  <- as.matrix(mtcars)
rc <- rainbow(nrow(x), start = 0, end = .3)
cc <- rainbow(ncol(x), start = 0, end = .3)
hv <- heatmap(x, col = cm.colors(256), scale = "column",
RowSideColors = rc, ColSideColors = cc, margins = c(5,10),
xlab = "specification variables", ylab =  "Car Models",
main = "heatmap(<Mtcars data>, ..., scale = \"column\")")
utils::str(hv) # the two re-ordering index vectors
rm(list = ls(all = TRUE)) #limpiar workspace
setwd("~/REDISTRICTING/ELECCION_2006")
# ---------
# Read data
# ---------
conteo <- read.table("poblacion_distrito_2005.txt", header=TRUE, sep=",")
computo <- read.table("DIPUTADOS_2006.txt", header=TRUE, sep="\t")
computo <- merge(computo,
data.frame(ENTIDAD.FEDERATIVA=levels(computo$ENTIDAD.FEDERATIVA),
Estado=c(1:4, 7:8, 5:6, 9:32)),
by="ENTIDAD.FEDERATIVA")
# Merge
data <- merge(computo, conteo[,1:3],
by.x=c("Estado", "DISTRITO"),
by.y=c("Estado", "Distrito"))
#----------
# Barplot
#----------
Mayoritarios <- rowSums(data[,c("PAN","ALIANZA.POR.MÉXICO","POR.EL.BIEN.DE.TODOS")])
df <- data.frame(Mayoritarios = Mayoritarios,
Minoritarios = data$TOTAL-Mayoritarios,
Lista.Nominal = data$LISTA.NOMINAL-data$TOTAL,
Poblacion = data$Pob_tot-data$LISTA.NOMINAL)
# paleta de color
require(RColorBrewer)
col <- brewer.pal(ncol(df), "Purples")
# Function
mi.plot <- function(x) {
barplot(x, xlab="(Miles)", col=col, horiz=TRUE, xlim=xlim, border=NA, cex.names=0.7)
axis(3)
abline(v=seq(0,500,100), col = "gray", lty = "dotted")
}
# Plot
pdf("barplot.pdf", 9, 8)
par(mfrow=c(1,2), yaxs="i", cex=.9)
# Ordenar por poblacion total
sorted.df <- df[order(data$Pob_tot),]/1000
rownames(sorted.df) <- 300:1
# Rango
xlim <- c(0, max(data$Pob_tot/1000))
# Plot
mi.plot(t(as.matrix(sorted.df[151:300,])))
mi.plot(t(as.matrix(sorted.df[1:150,])))
legend("bottomright", legend=rev(names(df)), pch=15, col=rev(col), bty="n", cex=0.8)
dev.off()
max(data$Poblacion)/min(data$Poblacion)
max(df$Poblacion)/min(df$Poblacion)
names(data)
max(df$Pob_tot)/min(df$Pob_tot)
summary(df$Pob_tot)
View(data)
max(data$Pob_tot)/min(data$Pob_tot)
max(data$Pob_tot)
Mayoritarios <- rowSums(data[,c("PAN","ALIANZA.POR.MÉXICO","POR.EL.BIEN.DE.TODOS")])
df <- data.frame(Mayoritarios = Mayoritarios,
Participacion = data$TOTAL-Mayoritarios,
Lista.Nominal = data$LISTA.NOMINAL-data$TOTAL,
Poblacion = data$Pob_tot-data$LISTA.NOMINAL)
# paleta de color
require(RColorBrewer)
col <- brewer.pal(ncol(df), "Purples")
# Function
mi.plot <- function(x) {
barplot(x, xlab="(Miles)", col=col, horiz=TRUE, xlim=xlim, border=NA, cex.names=0.7)
axis(3)
abline(v=seq(0,500,100), col = "gray", lty = "dotted")
}
# Plot
pdf("barplot.pdf", 9, 8)
par(mfrow=c(1,2), yaxs="i", cex=.9)
# Ordenar por poblacion total
sorted.df <- df[order(data$Pob_tot),]/1000
rownames(sorted.df) <- 300:1
# Rango
xlim <- c(0, max(data$Pob_tot/1000))
# Plot
mi.plot(t(as.matrix(sorted.df[151:300,])))
mi.plot(t(as.matrix(sorted.df[1:150,])))
legend("bottomright", legend=rev(names(df)), pch=15, col=rev(col), bty="n", cex=0.8)
dev.off()
max(data$TOTAL)/min(data$TOTAL)
names(data)
max(df$Participacion)/min(df$Participacion)
rm(list = ls(all = TRUE)) #limpiar workspace
setwd("~/REDISTRICTING/ELECCION_2006")
# ---------
# Read data
# ---------
conteo <- read.table("poblacion_distrito_2005.txt", header=TRUE, sep=",")
computo <- read.table("DIPUTADOS_2006.txt", header=TRUE, sep="\t")
computo <- merge(computo,
data.frame(ENTIDAD.FEDERATIVA=levels(computo$ENTIDAD.FEDERATIVA),
Estado=c(1:4, 7:8, 5:6, 9:32)),
by="ENTIDAD.FEDERATIVA")
# Merge
data <- merge(computo, conteo[,1:3],
by.x=c("Estado", "DISTRITO"),
by.y=c("Estado", "Distrito"))
#----------
# Barplot
#----------
Mayoritarios <- rowSums(data[,c("PAN","ALIANZA.POR.MÉXICO","POR.EL.BIEN.DE.TODOS")])
df <- data.frame(Mayoritarios = Mayoritarios,
Participacion = data$TOTAL-Mayoritarios,
Lista.Nominal = data$LISTA.NOMINAL-data$TOTAL,
Poblacion = data$Pob_tot-data$LISTA.NOMINAL)
# paleta de color
require(RColorBrewer)
col <- brewer.pal(ncol(df), "Purples")
# Function
mi.plot <- function(x) {
barplot(x, xlab="(Miles)", col=col, horiz=TRUE, xlim=xlim, border=NA, cex.names=0.7)
axis(3)
abline(v=seq(0,500,100), col = "gray", lty = "dotted")
}
# Plot
pdf("barplot.pdf", 9, 8)
par(mfrow=c(1,2), yaxs="i", cex=.9)
# Ordenar por poblacion total
sorted.df <- df[order(data$Pob_tot),]/1000
rownames(sorted.df) <- 300:1
# Rango
xlim <- c(0, max(data$Pob_tot/1000))
# Plot
mi.plot(t(as.matrix(sorted.df[151:300,])))
mi.plot(t(as.matrix(sorted.df[1:150,])))
legend("bottomright", legend=rev(names(df)), pch=15, col=rev(col), bty="n", cex=0.8)
dev.off()
#----------
# Density
#----------
partidos <- c("PAN","ALIANZA.POR.MÉXICO","POR.EL.BIEN.DE.TODOS","NUEVA.ALIANZA","ALTERNATIVA")
Ganador <- apply(data[,partidos], 1, function(x) names(which.max(x)))
Max <- apply(data[,partidos], 1, max)
mi.plot <- function(x, main) {
PAN <- density(x[Ganador=="PAN"])
PRI <- density(x[Ganador=="ALIANZA.POR.MÉXICO"])
PRD <- density(x[Ganador=="POR.EL.BIEN.DE.TODOS"])
xlim <- range(PAN$x, PRI$x, PRD$x)
ylim <- range(PAN$y, PRI$y, PRD$y)
plot(PAN, col="royalblue3", lwd=2, main=main, xlim=xlim, ylim=ylim)
lines(PRI, col="firebrick", lwd=2)
lines(PRD, col="gold3", lwd=2)
}
pdf("density.pdf", 7,7)
par(mfrow=c(2,2))
mi.plot(data$Pob_tot/1000, main="Población Total (miles)")
mi.plot(data$LISTA.NOMINAL/1000, main="Lista Nominal (miles)")
mi.plot(data$TOTAL/1000, main="Votantes (miles)")
mi.plot((data$TOTAL-Mayoritarios)/1000, main="Votos partidos minoritarios (miles)")
dev.off()
mi.plot(Max/1000, main="Votos del ganador (miles)")
sd(data$Pob_tot)
sd(data$LISTA.NOMINAL)
sd(data$TOTAL)
?corr
?correlate
?cor
cor(data$Pob_tot, data$TOTAL)
cor(data$LISTA.NOMINAL, data$TOTAL)
cor(Max, data$TOTAL)
summary(data)
336431/152121
504722/243902
?by
by(data$Pob_tot, Ganador, mean)
by(data$LISTA.NOMINAL, Ganador, mean)
by(data$TOTAL, Ganador, mean)
by(data$TOTAL-Mayoritarios, Ganador, mean)
by(data$Pob_tot/1000, Ganador, mean)
by(data$LISTA.NOMINAL/1000, Ganador, mean)
by(data$TOTAL/1000, Ganador, mean)
by((data$TOTAL-Mayoritarios)/1000, Ganador, mean)
80000/.533
rm(list = ls(all = TRUE)) #limpiar workspace
setwd("~/REDISTRICTING/ELECCION_2006")
# ---------
# Read data
# ---------
conteo <- read.table("poblacion_distrito_2005.txt", header=TRUE, sep=",")
computo <- read.table("DIPUTADOS_2006.txt", header=TRUE, sep="\t")
computo <- merge(computo,
data.frame(ENTIDAD.FEDERATIVA=levels(computo$ENTIDAD.FEDERATIVA),
Estado=c(1:4, 7:8, 5:6, 9:32)),
by="ENTIDAD.FEDERATIVA")
# Merge
data <- merge(computo, conteo[,1:3],
by.x=c("Estado", "DISTRITO"),
by.y=c("Estado", "Distrito"))
#----------
# Barplot
#----------
Mayoritarios <- rowSums(data[,c("PAN","ALIANZA.POR.MÉXICO","POR.EL.BIEN.DE.TODOS")])
df <- data.frame(Minoritarios = data$TOTAL-Mayoritarios,
Participacion = data$TOTAL-Mayoritarios,
Lista.Nominal = data$LISTA.NOMINAL-data$TOTAL,
Poblacion = data$Pob_tot-data$LISTA.NOMINAL)
# paleta de color
require(RColorBrewer)
col <- brewer.pal(ncol(df), "Purples")
# Function
mi.plot <- function(x) {
barplot(x, xlab="(Miles)", col=col, horiz=TRUE, xlim=xlim, border=NA, cex.names=0.7)
axis(3)
abline(v=seq(0,500,100), col = "gray", lty = "dotted")
}
# Plot
pdf("barplot.pdf", 9, 8)
par(mfrow=c(1,2), yaxs="i", cex=.9)
# Ordenar por poblacion total
sorted.df <- df[order(data$Pob_tot),]/1000
rownames(sorted.df) <- 300:1
# Rango
xlim <- c(0, max(data$Pob_tot/1000))
# Plot
mi.plot(t(as.matrix(sorted.df[151:300,])))
mi.plot(t(as.matrix(sorted.df[1:150,])))
legend("bottomright", legend=rev(names(df)), pch=15, col=rev(col), bty="n", cex=0.8)
dev.off()
rm(list = ls(all = TRUE)) #limpiar workspace
setwd("~/REDISTRICTING/ELECCION_2006")
# ---------
# Read data
# ---------
conteo <- read.table("poblacion_distrito_2005.txt", header=TRUE, sep=",")
computo <- read.table("DIPUTADOS_2006.txt", header=TRUE, sep="\t")
computo <- merge(computo,
data.frame(ENTIDAD.FEDERATIVA=levels(computo$ENTIDAD.FEDERATIVA),
Estado=c(1:4, 7:8, 5:6, 9:32)),
by="ENTIDAD.FEDERATIVA")
# Merge
data <- merge(computo, conteo[,1:3],
by.x=c("Estado", "DISTRITO"),
by.y=c("Estado", "Distrito"))
#----------
# Barplot
#----------
Mayoritarios <- rowSums(data[,c("PAN","ALIANZA.POR.MÉXICO","POR.EL.BIEN.DE.TODOS")])
df <- data.frame(Minoritarios = data$TOTAL-Mayoritarios,
Participacion = data$TOTAL-Minoritarios,
Lista.Nominal = data$LISTA.NOMINAL-data$TOTAL,
Poblacion = data$Pob_tot-data$LISTA.NOMINAL)
# paleta de color
require(RColorBrewer)
col <- brewer.pal(ncol(df), "Purples")
# Function
mi.plot <- function(x) {
barplot(x, xlab="(Miles)", col=col, horiz=TRUE, xlim=xlim, border=NA, cex.names=0.7)
axis(3)
abline(v=seq(0,500,100), col = "gray", lty = "dotted")
}
# Plot
pdf("barplot.pdf", 9, 8)
par(mfrow=c(1,2), yaxs="i", cex=.9)
# Ordenar por poblacion total
sorted.df <- df[order(data$Pob_tot),]/1000
rownames(sorted.df) <- 300:1
# Rango
xlim <- c(0, max(data$Pob_tot/1000))
# Plot
mi.plot(t(as.matrix(sorted.df[151:300,])))
mi.plot(t(as.matrix(sorted.df[1:150,])))
legend("bottomright", legend=rev(names(df)), pch=15, col=rev(col), bty="n", cex=0.8)
dev.off()
rm(list = ls(all = TRUE)) #limpiar workspace
setwd("~/REDISTRICTING/ELECCION_2006")
# ---------
# Read data
# ---------
conteo <- read.table("poblacion_distrito_2005.txt", header=TRUE, sep=",")
computo <- read.table("DIPUTADOS_2006.txt", header=TRUE, sep="\t")
computo <- merge(computo,
data.frame(ENTIDAD.FEDERATIVA=levels(computo$ENTIDAD.FEDERATIVA),
Estado=c(1:4, 7:8, 5:6, 9:32)),
by="ENTIDAD.FEDERATIVA")
# Merge
data <- merge(computo, conteo[,1:3],
by.x=c("Estado", "DISTRITO"),
by.y=c("Estado", "Distrito"))
#----------
# Barplot
#----------
Mayoritarios <- rowSums(data[,c("PAN","ALIANZA.POR.MÉXICO","POR.EL.BIEN.DE.TODOS")])
df <- data.frame(Mayoritarios = Mayoritarios,
Participacion = data$TOTAL-Mayoritarios,
Lista.Nominal = data$LISTA.NOMINAL-data$TOTAL,
Poblacion = data$Pob_tot-data$LISTA.NOMINAL)
# paleta de color
require(RColorBrewer)
col <- brewer.pal(ncol(df), "Purples")
# Function
mi.plot <- function(x) {
barplot(x, xlab="(Miles)", col=col, horiz=TRUE, xlim=xlim, border=NA, cex.names=0.7)
axis(3)
abline(v=seq(0,500,100), col = "gray", lty = "dotted")
}
# Plot
pdf("barplot.pdf", 9, 8)
par(mfrow=c(1,2), yaxs="i", cex=.9)
# Ordenar por poblacion total
sorted.df <- df[order(data$Pob_tot),]/1000
rownames(sorted.df) <- 300:1
# Rango
xlim <- c(0, max(data$Pob_tot/1000))
# Plot
mi.plot(t(as.matrix(sorted.df[151:300,])))
mi.plot(t(as.matrix(sorted.df[1:150,])))
legend("bottomright", legend=rev(names(df)), pch=15, col=rev(col), bty="n", cex=0.8)
dev.off()
#----------
# Density
#----------
partidos <- c("PAN","ALIANZA.POR.MÉXICO","POR.EL.BIEN.DE.TODOS","NUEVA.ALIANZA","ALTERNATIVA")
Ganador <- apply(data[,partidos], 1, function(x) names(which.max(x)))
Max <- apply(data[,partidos], 1, max)
mi.plot <- function(x, main) {
PAN <- density(x[Ganador=="PAN"])
PRI <- density(x[Ganador=="ALIANZA.POR.MÉXICO"])
PRD <- density(x[Ganador=="POR.EL.BIEN.DE.TODOS"])
xlim <- range(PAN$x, PRI$x, PRD$x)
ylim <- range(PAN$y, PRI$y, PRD$y)
plot(PAN, col="royalblue3", lwd=2, main=main, xlim=xlim, ylim=ylim)
lines(PRI, col="firebrick", lwd=2)
lines(PRD, col="gold3", lwd=2)
}
pdf("density.pdf", 7,7)
par(mfrow=c(2,2))
mi.plot(data$Pob_tot/1000, main="Población Total (miles)")
mi.plot(data$LISTA.NOMINAL/1000, main="Lista Nominal (miles)")
mi.plot(data$TOTAL/1000, main="Votantes (miles)")
mi.plot((data$TOTAL-Mayoritarios)/1000, main="Votos partidos minoritarios (miles)")
dev.off()
summary(data)
